<template>
    <form-container title="编辑" :width="700" :visible="visible" :destroy-on-close="true" @close="onClose">
        <a-form ref="formRef" :model="formState" :rules="formRules" layout="vertical">
            <a-form-item label="编码" name="id">
                <a-input v-model:value="formState.id" allow-clear />
            </a-form-item>
            <a-form-item label="工单" name="planId">
                <a-select v-model:value="formState.planId" placeholder="请选择">
                    <a-select-option v-for="item in productPlan" :key="item.id" :value="item.id">{{ item.id
                    }}</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="员工" name="employeeCode">
                <a-select v-model:value="formState.employeeCode" placeholder="请选择">
                    <a-select-option v-for="item in employee" :key="item.employeeCode" :value="item.employeeCode">{{
                        item.employeeName }}</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="工序" name="processCode">
                <a-select v-model:value="formState.processCode" placeholder="请选择产线">
                    <a-select-option v-for="item in process" :key="item.processCode" :value="item.processCode">{{
                        item.processName
                    }}</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="实际生产数量" name="actualQty">
                <a-input v-model:value="formState.actualQty" placeholder="请输入名称" allow-clear />
            </a-form-item>
            <a-form-item label="报工时间" name="reportTime">
                <a-date-picker v-model:value="formState.reportTime" show-time type="date" placeholder="Pick a date"
                    style="width: 100%" />
            </a-form-item>
            <a-form-item label="状态" name="status">
                <a-select v-model:value="formState.status" placeholder="请选择产线">
                    <a-select-option :value="1">审核中</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="审核信息" name="auditRemark">
                <a-input v-model:value="formState.auditRemark" placeholder="请输入名称" allow-clear />
            </a-form-item>
        </a-form>
        <template #footer>
            <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
            <a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
        </template>
    </form-container>
</template>

<script setup name="updatePassword">
import { required } from '@/utils/formRules'
import tool from '@/utils/tool'
import productionApi from '@/api/production/ticketManagementaApi'
import workReporterApi from '@/api/production/workReporterApi'
import employeeApi from '@/api/production/employeeApi'
import processApi from '@/api/mes/processinfo/processApi'

// 定义emit事件
const emit = defineEmits({ successful: null })
// 默认是关闭状态
let visible = ref(false)
const formRef = ref()
// 表单数据
const formState = ref({})
const submitLoading = ref(false)

// 打开抽屉
const onOpen = (email) => {
    visible.value = true
    formState.value = email
}
// 关闭抽屉
const onClose = () => {
    emit('successful')
    visible.value = false
}
// 默认要校验的
const rules = {
    name: [required('请输入名称')]
}

// 提交数据
const onSubmit = async () => {
    formRef.value.validateFields().then(() => {
        submitLoading.value = true
        let fromData = {
            id: formState.value.id,
            productCode: formState.value.productCode,
            productName: formState.value.productName,
            productionLineCode: formState.value.productionLineCode,
            plannedQty: formState.value.plannedQty,
            isSkipProcess: formState.value.isSkipProcess,
            annex: formState.value.annex,
            remark: formState.value.remark,
            startTime: formState.value.startTime,
            endTime: formState.value.endTime,
            status: formState.value.status
        }
        // 提交数据
        workReporterApi
            .updateWorkReporter(fromData)
            .then(() => {
                //formRef.value.resetFields()
                visible.value = false
                emit('successful', fromData)
            })
            .finally(() => {
                submitLoading.value = false
                callback() //调用组件中的回调方法
            })
    })
}

//获取工单
const productPlan = ref([])
const queryProductPlan = () => {
    productionApi.bindTicket({}).then(res => {
        productPlan.value = res
    })
}

//获取员工
const employee = ref([])
const queryEmployee = () => {
    employeeApi.bindEmployee({}).then(res => {
        employee.value = res
    })
}

//获取工序
const process = ref([])
const queryProcess = () => {
    processApi.bindProcess({}).then(res => {
        process.value = res
    })
}

onMounted(() => {
    queryProductPlan();
    queryEmployee();
    queryProcess();
})

//类型
const processTypeOptions = tool.dictList('PROCESS_TYPE')
// 报工模式
const processModeOptions = tool.dictList('PROCESS_MODE')

// 调用这个函数将子组件的一些数据和方法暴露出去
defineExpose({
    onOpen
})
</script>

<style scoped></style>
